package com.migo.thread;

import com.migo.entity.ShOrderInfoEntity;
import com.migo.oss.OSSFactory;
import com.migo.service.ShOrderInfoService;
import com.migo.utils.DateUtil;
import com.migo.utils.ImageBase64Utils;
import com.migo.utils.JsonUtils;
import com.migo.utils.MakeOrderNumUtil;
import net.sf.json.JSONArray;
import sun.misc.BASE64Decoder;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Wes on 2018/12/5.
 */
public class AfterSaleThread implements Runnable {

    private ShOrderInfoService shOrderInfoService;
    private String params;
    private OSSFactory ossFactory;

    public AfterSaleThread(String params, ShOrderInfoService shOrderInfoService, OSSFactory ossFactory) {
        this.params = params;
        this.shOrderInfoService = shOrderInfoService;
        this.ossFactory = ossFactory;
    }

    public static Integer getEquipmentType(String code) {
        Integer equipmentType = 0;
        if (code != null && !"".equals(code)) {
            if (code.substring(3, 4).equalsIgnoreCase("A")) {
                equipmentType = 1;
            }
            if (code.substring(3, 4).equalsIgnoreCase("B")) {
                equipmentType = 2;
            }
            if (code.substring(3, 4).equalsIgnoreCase("C")) {
                equipmentType = 3;
            }
            if (code.substring(3, 4).equalsIgnoreCase("D")) {
                equipmentType = 4;
            }
            if (code.substring(3, 4).equalsIgnoreCase("E")) {
                equipmentType = 5;
            }

            //MG10 识别code
            if (code.substring(3, 4).equalsIgnoreCase("G")) {
                equipmentType = 7;
            }
        }
        return equipmentType;
    }

    @Override
    public void run() {
        try {
            Map<String, String> mapFromJson = JsonUtils.getMapFromJson(this.params);
            JSONArray array = JSONArray.fromObject(mapFromJson.get("images"));
            StringBuffer imageBuffer = new StringBuffer();
            int len = array.size();
            for (int j = 0; j < len; j++) {
                String imageType = ImageBase64Utils.getImageType(String.valueOf(array.get(j)));
                String imageStr = ImageBase64Utils.replacePre(String.valueOf(array.get(j)));
                System.out.print(imageType + "-----------" + imageStr);
                BASE64Decoder decoder = new BASE64Decoder();
                // Base64解码
                byte[] b = decoder.decodeBuffer(imageStr);
                for (int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {// 调整异常数据
                        b[i] += 256;
                    }
                }
                // 生成jpeg图片
                String imgFilePath = UUID.randomUUID().toString() + "." + imageType;// 新生成的图片
                String url = this.ossFactory.build().upload(imgFilePath, b);
                imageBuffer.append(url);
                if (j < len - 1) {
                    imageBuffer.append(",");
                }
                System.out.print("-----------" + url);
            }
            String userId = mapFromJson.get("userId");
            String proId = mapFromJson.get("proId");
            String userName = mapFromJson.get("userName");
            String emailOrPhone = mapFromJson.get("emailOrPhone");
            String serviceType = mapFromJson.get("serviceType");
            String faultType = mapFromJson.get("faultType");  //保留字段
            String faultFrequency = mapFromJson.get("faultFrequency");//保留字段
            String faultImpact = mapFromJson.get("faultImpact");//保留字段
            String area = mapFromJson.get("area");
            switch (area) {
                case "cn":
                    System.out.println("cn");
                    break;
                case "jp":
                    System.out.println("jp");
                    break;
                default:
                    area = "en";
            }
            String qstDescription = mapFromJson.get("qstDescription");

            ShOrderInfoEntity shOrderInfo = new ShOrderInfoEntity();
            shOrderInfo.setCountryArea(area);
            if (emailOrPhone.contains("@")) {
                shOrderInfo.setUserEmail(emailOrPhone);
            } else {
                shOrderInfo.setUserPhone(emailOrPhone);
            }
            shOrderInfo.setUserName(userName);
            shOrderInfo.setDeviceCode(proId);
            shOrderInfo.setUserId(userId);
            shOrderInfo.setProId(getEquipmentType(proId));
            shOrderInfo.setAddTime(DateUtil.getNow());
            shOrderInfo.setServiceTypeId(Integer.parseInt(serviceType));
            shOrderInfo.setOrderPrice(0.00);
            shOrderInfo.setPartsStatus(1);
            shOrderInfo.setPreviousStatus(0);
            shOrderInfo.setNormsNumber(0);
            shOrderInfo.setQuestionId("0");
            shOrderInfo.setRepairPrice(0.00);
            shOrderInfo.setDutyType(0);
            shOrderInfo.setInsuranceId(0);
            shOrderInfo.setOrderDetail("");
            shOrderInfo.setQuestionImagePath(imageBuffer.toString());
            shOrderInfo.setSingleId(UUID.randomUUID()+"");
            shOrderInfo.setStatus(1);
            shOrderInfo.setOrderStatusId(1);
            shOrderInfo.setQuestionDescription(qstDescription);
            MakeOrderNumUtil m = new MakeOrderNumUtil();
            shOrderInfo.setOrderId(m.makeOrderNum());
            System.out.println("userId:  "+userId);
            this.shOrderInfoService.save(shOrderInfo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
